/**
 * 
 */

shop = window.shop || {}

shop.addProduct = function(id)
{
	var str = $("#quantity" + id).val();
	var pattern = "[0-9]+";
	var result = str.match(pattern);
	
	if (str.match(pattern))
	{
		$.ajax({
	        url: 'ShopAPI.php',
	        type: 'POST',
	        data: {
	        	productId: id,
	        	quantity: $("#quantity" + id).val()
	        	},
	        success: function(data) {
	             alert("Ihre Produkte wurden dem Warenkorb hinzugefügt.");
	        },
	        fail: function(data)
	        {
	        	alert("Es ist ein Fehler aufgetreten.");
	        }
	        	
	    });
	}
	else
	{
		alert("Sie dürfen nur Zahlen eingeben!");
	}
	
}

shop.showLogin = function() {
	$("#loginForm").fadeIn("slow");
}

shop.login = function() {
	$.ajax({
        url: 'ShopAPI.php',
        type: 'POST',
        data: {
        	password: $('#password').val(),
        	username: $('#username').val()
        	},
        success: function(data) {
    		if (data == "1")
    		{
    			$("#login").css("display", "none");
        		$("#logout").css("display", "block");
        		$("#loginForm").fadeOut("slow");
    		}
    		else
    		{
    			alert("Passwort oder Benutername nicht korrekt!");
    			$("#login").css("display", "block");
        		$("#logout").css("display", "none");
    		}
        },
        fail: function(data)
        {
        	$("body").append("<span>" + data + "</span>");
        }	
    });
}

shop.logout = function() {
	$.ajax({
        url: 'ShopAPI.php',
        type: 'POST',
        data: {
        	logout: 1
        	},
        success: function(data) {
    		$("#login").css("display", "block");
    		$("#logout").css("display", "none");
        },
        fail: function(data)
        {
        	$("body").append("<span>" + data + "</span>");
        }
        	
    });
}

$(document).ready(function() {
	$.ajax({
        url: 'ShopAPI.php',
        type: 'POST',
        data: {
        	checkAuth: 1
        	},
        success: function(data) {
        	if (data == "1")
        	{
        		$("#login").css("display", "none");
        		$("#logout").css("display", "block");
        	}
        	else
        	{
        		$("#login").css("display", "block");
        		$("#logout").css("display", "none");
        	}
        },
        fail: function(data)
        {
        	$("body").append("<span>" + data + "</span>");
        }
        	
    });
});